﻿namespace Tszy.Unlimited.Base.Collections;

/// <summary>
/// URL 参数字典。
/// </summary>
public class UrlDictionary
{
    /// <summary>
    /// 字典信息。
    /// </summary>
    private readonly MonoConcurrentDictionary<string, string> _dictionary = new();

    /// <summary>
    ///
    /// </summary>
    public ICollection<string> Keys
    {
        get { return _dictionary.Keys; }
    }

    /// <summary>
    ///
    /// </summary>
    public ICollection<string> Values
    {
        get { return _dictionary.Values; }
    }

    /// <summary>
    ///
    /// </summary>
    public int Count
    {
        get { return _dictionary.Count; }
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public string this[string key]
    {
        get
        {
            if (string.IsNullOrWhiteSpace(key))
                return string.Empty;

            key = key.ToLower();

            return _dictionary.ContainsKey(key) ? _dictionary[key] ?? string.Empty : string.Empty;
        }
        set
        {
            if (string.IsNullOrWhiteSpace(key))
                return;

            key = key.ToLower();

            _dictionary[key] = value;
        }
    }

    /// <summary>
    /// 判断元素是否存在。
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public bool ContainsKey(string key)
    {
        if (string.IsNullOrWhiteSpace(key))
            return false;

        key = key.ToLower();

        return _dictionary.ContainsKey(key);
    }

    ///// <summary>
    /////
    ///// </summary>
    ///// <param name="value"></param>
    ///// <returns></returns>
    //public bool ContainsValue(string value)
    //{
    //    return _dictionary.ContainsValue(value);
    //}

    /// <summary>
    /// 增加元素。
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public bool Add(string key, string value)
    {
        if (string.IsNullOrWhiteSpace(key))
            return false;

        if (string.IsNullOrWhiteSpace(value))
            return false;

        key = key.ToLower();

        return _dictionary.TryAdd(key, value);
        //_dictionary.Add(key, value);
    }

    /// <summary>
    /// 移除指定 KEY 的元素。
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns>是否成功。</returns>
    public bool Remove(string key, out string? value)
    {
        value = string.Empty;

        if (string.IsNullOrWhiteSpace(key))
            return false;

        key = key.ToLower();

        return _dictionary.TryRemove(key, out value);
    }

    /// <summary>
    /// 获得元素。
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public bool TryGetValue(string key, out string? value)
    {
        if (string.IsNullOrWhiteSpace(key))
        {
            value = null;
            return false;
        }

        key = key.ToLower();

        return _dictionary.TryGetValue(key, out value);
    }

    /// <summary>
    ///
    /// </summary>
    public void Clear()
    {
        _dictionary.Clear();
    }
}
